using PlayMyLife.Present;
using PlayMyLife.ViewCommon;
using PlayMyLife.Events;

using TMPro;

using UnityEngine;
using ArmyAnt.ProcessController;
using PlayMyLife.ViewModel;
using PlayMyLife.View.Prefabs.Items;

namespace PlayMyLife.View.Prefabs {
    public class GameMainPanel : FormBase {
        [Header("地图")]
        [SerializeField] private GameObject placePanel;
        [SerializeField] private TextMeshProUGUI placeName;
        [SerializeField] private RectTransform placeParent;
        [SerializeField] private GameObject prefabPlaceItem;
        [SerializeField] private RectTransform placePeopleParent;
        [SerializeField] private GameObject prefabPeopleItem;
        [Header("对话框")]
        [SerializeField] private GameObject conversationPanel;
        [SerializeField] private TextMeshProUGUI conversationTitle;
        [SerializeField] private TextMeshProUGUI conversationText;
        [SerializeField] private GameObject conversationMask;
        [Header("选项框")]
        [SerializeField] private GameObject choiseMask;
        [SerializeField] private GameObject choisePanel;
        [SerializeField] private TextMeshProUGUI choiseTitle;
        [SerializeField] private RectTransform choiseParent;
        [SerializeField] private GameObject prefabChoiceItem;


        private IOperaDataReader director;

        private long nextConversationEventId;

        public void OnClickConversation() {
            if(GameViewManager.EnableOperate) {
                GameViewManager.EventMgr.NotifyAsync(new EventArgs_ConversationStepOn(nextConversationEventId));
            }
        }

        protected override void OnOpen() {
            if(Arguments is EventArgs_ShowPanel_WithDirectorReader args) {
                director = args.Director;
            } else {
                throw new System.ArgumentException();
            }

            placePanel.SetActive(false);
            choiseMask.SetActive(false);
            choisePanel.SetActive(false);
            conversationPanel.SetActive(false);
            conversationMask.SetActive(false);

            GameViewManager.EventMgr.Listen(DirectorEvent.ShowPlace, OnShowPlace);
            GameViewManager.EventMgr.Listen(DirectorEvent.ShowConversation, OnShowConversation);
            GameViewManager.EventMgr.Listen(DirectorEvent.ShowChoices, OnShowChoice);
            GameViewManager.EventMgr.Listen(DirectorEvent.ConversationStepOn, OnConversationStepOn, 0);
        }

        protected override void OnClose() {
            GameViewManager.EventMgr.Unlisten(DirectorEvent.ShowPlace, OnShowPlace);
            GameViewManager.EventMgr.Unlisten(DirectorEvent.ShowConversation, OnShowConversation);
            GameViewManager.EventMgr.Unlisten(DirectorEvent.ShowChoices, OnShowChoice);
            GameViewManager.EventMgr.Unlisten(DirectorEvent.ConversationStepOn, OnConversationStepOn);
        }

        private void Awake() {

        }

        private void OnShowPlace(IEventManager<DirectorEvent>.IEventArgs e) {
            if(e is EventArgs_ShowPlace arg) {
                var placeData = director.PlaceReader.GetPlaceData(arg.PlaceId);
                placeParent.DetachChildren();
                placeName.text = placeData.name;
                foreach(var i in placeData.items) {
                    var item = Instantiate(prefabPlaceItem, placeParent);
                    var transform = item.GetComponent<RectTransform>();
                    transform.localScale = new Vector3(1, 1, 1);
                    transform.anchoredPosition = new Vector2(i.x, i.y);
                    transform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, i.width);
                    transform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, i.height);
                    var comp = item.GetComponent<PlaceItem>();
                    comp.text.text = i.name;
                    comp.eventId = i.eventId;
                }
                // 显示所有的人
                var people = director.PlaceReader.GetAllPersonsAtPlace(arg.PlaceId);
                placePeopleParent.DetachChildren();
                foreach(var p in people) {
                    var data = director.RolesReader.GetRoleData(p);
                    var item = Instantiate(prefabPeopleItem, placePeopleParent);
                    item.transform.localScale = new Vector3(1, 1, 1);
                    var comp = item.GetComponent<PersonItem>();
                    comp.textName.text = data.baseProperty.name;
                    comp.roleId = data.id;
                }
            }
        }

        private void OnShowConversation(IEventManager<DirectorEvent>.IEventArgs e) {
            if(e is EventArgs_ShowConversation arg) {
                conversationPanel.SetActive(true);
                conversationMask.SetActive(true);
                var role = director.RolesReader.GetRoleData(arg.RoleId);
                conversationTitle.text = role.baseProperty.name;
                conversationText.text = arg.Content;
                nextConversationEventId = arg.NextEventId;
            }
        }

        private void OnShowChoice(IEventManager<DirectorEvent>.IEventArgs e) {
            if(e is EventArgs_ShowChoices arg) {
                choiseMask.SetActive(true);
                choisePanel.SetActive(true);
                choiseParent.DetachChildren();
                choiseTitle.text = arg.Title;
                for(int i = 0; i < arg.Items.Length; ++i) {
                    var item = Instantiate(prefabChoiceItem, choiseParent);
                    item.transform.localScale = new Vector3(1, 1, 1);
                    var comp = item.GetComponent<ChoiceItem>();
                    comp.text.text = arg.Items[i];
                    comp.eventId = arg.Events[i];
                }
            }
        }

        private void OnConversationStepOn(IEventManager<DirectorEvent>.IEventArgs e) {
            if(e is EventArgs_ConversationStepOn arg) {
                conversationMask.SetActive(false);
                conversationPanel.SetActive(false);
            }
        }
    }
}
